<?php

class Admin_Table_Auction_LotHistory extends Apk_Db_Table {

    protected $_name = 'auction_history';
    protected $_primary = 'id';
    public $_ERROR = "";

    public function GetError() {
        return $this->_ERROR;
    }

    public function getDataList($page = 1, $sort = null, $page_count = null, $filters = array()) {
        $sel_inner = $this->getAdapter()->select()
                ->from($this->_name, array($this->_name . '.id', 'add_date','comment','reason'))
                ->joinLeft(array("accounts"), "accounts.id = " . $this->_name . ".add_user_id", array('user' => "concat(accounts.last_name, ' ', accounts.first_name, ' ', accounts.patronymic_name)"))
                ->joinLeft('auction_lots_status', "auction_lots_status.id = ".$this->_name.".id_status", array('status' => "name"));
                //->joinLeft('auction_status_reasons', "auction_status_reasons.id = id_status_reason", array('reason' => "name"));

        if(count($filters) > 0)
	        foreach($filters as $k => $v)
	        {
	            switch ($k)
	            {
	                case 'id_lot':
	                	if(intval($v)) $sel_inner->where($k." = ?", intval($v));
	                	break;
                        case 'id_auction':
	                	if(intval($v)) $sel_inner->where($k." = ?", intval($v));
	                	break;
	            }
	        }
        
        $pieces = preg_split("/\s/u", $sort);
        switch ($pieces[0]) {
        	case 'id':
        		$sel_inner->order($this->_name.".id " . $pieces[1]);
        		break;
            case 'add_date':
                $sel_inner->order("add_date " . $pieces[1]);
                break;
            default:
                $sel_inner->order(array("add_date DESC"));
                break;
        }

        $sel = $this->getAdapter()->select()->from($sel_inner, array('id', 'add_date', 'user', 'comment', 'status', 'reason'));
                
        return $this->setDecorator('simpleDecorator')->getPager($sel, $page, array('perPage' => $page_count));
    }

    
	/********************
     ***  Decorators  ***
    *********************/

    public function simpleDecorator($key, $value)
    {
    	//$result[$key]='';
    	switch ($key) {
            case 'ins_date':
            case 'end_date':
            case 'deadline_date':
            case 'add_date':
            case 'modify_date':
            case 'price_date':
                if($value=="" || $value===null){
                    $result[$key] = '';
                }else{
                    $date = new DateTime($value);
                    $result[$key] = $date->format('d.m.Y');
                }
                break;
            case 'start_date':
    	 		if($value=="" || $value===null){
                    $result[$key] = '';
                }else{
                    $date = new DateTime($value);
                    $result[$key] = $date->format('d.m.Y H:i');
                }
            	break;
            case 'public_date':
            case 'archive_date':
            	$result[$key]['__attributes'] = $this->timestampToArray($value);
            	break;
            case 'town':
            case 'region':
            case 'district':
            	$result[$key] = $this->convertGeomNameStr($value);
            	break;
            default:
                $result[$key] = stripslashes($value);
                break;
        }
        return $result;
    }
}